﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for EmployeeHandler
    /// </summary>
    public class EmployeeHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            string FirstName = HttpContext.Current.Request.QueryString["FirstName"];
            //string LastName = HttpContext.Current.Request.QueryString["LastName"];
            int DepartmentID = HttpContext.Current.Request.QueryString["DepartmentID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["DepartmentID"]);
            int SectionID = HttpContext.Current.Request.QueryString["SectionID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SectionID"]);
            int SubSectionID = HttpContext.Current.Request.QueryString["SubSectionID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SubSectionID"]);
            int SuperiorID = HttpContext.Current.Request.QueryString["SuperiorID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SuperiorID"]);
            int JobPositionID = HttpContext.Current.Request.QueryString["JobPositionID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["JobPositionID"]);
            string JobTitle = HttpContext.Current.Request.QueryString["JobTitle"];
            string Email = HttpContext.Current.Request.QueryString["Email"];
            int SubBranchID = HttpContext.Current.Request.QueryString["SubBranchID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SubBranchID"]);
            int PrivilegeID = HttpContext.Current.Request.QueryString["PrivilegeID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["PrivilegeID"]);
            string Phone = HttpContext.Current.Request.QueryString["Phone"];
            string Ext = HttpContext.Current.Request.QueryString["Ext"];
            string Password = HttpContext.Current.Request.QueryString["Password"];
            bool isAdmin = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isAdmin"]);
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            DateTime? JoinDate = HttpContext.Current.Request.QueryString["JoinDate"] == "" ? (DateTime?)null : Convert.ToDateTime(HttpContext.Current.Request.QueryString["JoinDate"]);
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            bool isLock = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isLock"]);
            string NIK = HttpContext.Current.Request.QueryString["NIK"];

            EmployeeFacade ef = new EmployeeFacade();
            EmployeeValidator ev = new EmployeeValidator();
            sysLog objSysLog = new sysLog();
            sysLogFacade sysLogFacade = new sysLogFacade();

            try
            {
                Employee Employees = ef.GetEmployeeByID(ID);
                if (Employees == null)
                {
                    Employees = new Employee();
                    Employees.ID = ID;
                    Employees.FirstName = FirstName;
                    Employees.LastName = "";// LastName;
                    Employees.Department = new DepartmentFacade().GetDepartmentByID(DepartmentID);
                    Employees.Section = new SectionFacade().GetSectionByID(SectionID);
                    Employees.SubSection = new SubSectionFacade().GetSubSectionByID(SubSectionID);
                    Employees.Supervisor = SuperiorID; //new EmployeeFacade().GetEmployeeByID(SuperiorID);
                    Employees.JobPosition = new JobPositionFacade().GetJobPositionByID(JobPositionID);
                    Employees.JobTitle = JobTitle;
                    Employees.Email = Email;
                    Employees.SubBranch = new SubBranchFacade().GetSubBranchByID(SubBranchID);
                    Employees.Privilege = new PrivilegeFacade().GetPrivilegeByID(PrivilegeID);
                    Employees.Phone = Phone;
                    Employees.Ext = Ext;
                    Employees.isAdmin = isAdmin;
                    Employees.isActive = isActive;
                    Employees.JoinDate = JoinDate;
                    Employees.Password = System.Configuration.ConfigurationManager.AppSettings["DefaultPassword"].ToString();
                    Employees.CreateBy = CurrUser.Email;
                    Employees.isLock = isLock;
                    Employees.NIK = NIK;
                    msg = ev.ValidateOnSave(Employees);
                }
                else if (action == "saveinfo")
                {
                    Employees.FirstName = FirstName;
                    Employees.LastName = "";// LastName;
                    Employees.Phone = Phone;
                    Employees.LastModifiedBy = Employees.Email;
                }
                else if (action == "changepassword")
                {
                    Employees.Password = Password;
                    Employees.LastModifiedBy = Employees.Email;
                }
                else if (action != "delete" && action != "reset")
                {
                    Employees.FirstName = FirstName;
                    Employees.LastName = "";// LastName;
                    Employees.Department = new DepartmentFacade().GetDepartmentByID(DepartmentID);
                    Employees.Section = new SectionFacade().GetSectionByID(SectionID);
                    Employees.SubSection = new SubSectionFacade().GetSubSectionByID(SubSectionID);
                    Employees.Supervisor = SuperiorID;// new EmployeeFacade().GetEmployeeByID(SuperiorID);
                    Employees.JobPosition = new JobPositionFacade().GetJobPositionByID(JobPositionID);
                    Employees.JobTitle = JobTitle;
                    Employees.Email = Email;
                    Employees.SubBranch = new SubBranchFacade().GetSubBranchByID(SubBranchID);
                    Employees.Privilege = new PrivilegeFacade().GetPrivilegeByID(PrivilegeID);
                    Employees.Phone = Phone;
                    Employees.Ext = Ext;
                    Employees.isAdmin = isAdmin;
                    Employees.isActive = isActive;
                    Employees.JoinDate = JoinDate;
                    Employees.LastModifiedBy = CurrUser.Email;
                    Employees.isLock = isLock;
                    Employees.NIK = NIK;
                    msg = ev.ValidateOnUpdate(Employees);
                }
                else if (action == "delete")
                    Employees.LastModifiedBy = CurrUser.Email;

                if (msg == "OK")
                {
                    using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        switch (action)
                        {
                            case "save":
                                msg = ef.Save(Employees);
                                break;
                            case "saveinfo":
                                msg = ef.Save(Employees);
                                break;
                            case "delete":
                                msg = ef.BeforeDelete(Employees);
                                if (msg == "0")
                                    msg = ef.Delete(Employees);
                                else
                                    msg = "Data tidak bisa di hapus,dipakai pada tabel yang lain !";
                                break;
                            case "reset":
                                msg = ef.UpdatePassword(Employees, string.Empty);
                                break;
                            case "changepassword":
                                Employee data = ef.GetEmployeeLogin(Employees.Email, Password);
                                if (data != null)
                                    msg = ef.UpdatePassword(Employees, Password);
                                else
                                    msg = "Please enter correct current password";
                                break;
                        }
                        trans.Complete();
                        trans.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                objSysLog.Description = ex.Message;
                objSysLog.LogEvent = "Input Employee";
                objSysLog.Source = "EmployeeHandler.ashx";
                objSysLog.CreatedBy = CurrUser.Email;
                objSysLog.CreatedDate = DateTime.Now;
                sysLogFacade.Save(objSysLog);
            }

            return msg;
        }
    }
}